package com.tsmti.lab.controller.system;

import com.tsmti.core.common.controller.BaseController;
import com.tsmti.core.common.model.AjaxJson;
import com.tsmti.core.util.ArrayUtils;
import com.tsmti.core.util.ConvertUtil;
import com.tsmti.core.util.StringUtils;
import com.tsmti.system.dto.PageColDto;
import com.tsmti.system.entity.SystemPageColumn;
import com.tsmti.system.entity.UserPageColumn;
import com.tsmti.system.service.SystemPageColumnService;
import com.tsmti.system.service.UserPageColumnService;
import com.tsmti.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Package：com.tsmti.lab.controller.system
 * @Class：ColumnController
 * @Description： TODO
 * @Author：zcw
 * @Date：Created in 2018/6/1 16:29
 * @Company:
 * @Version：
 * @Modified By:
 */
@Controller
@RequestMapping("/system/column")
public class ColumnController extends BaseController{
    @Autowired
    private SystemPageColumnService systemPageColumnService;

    @Autowired
    private UserPageColumnService userPageColumnService;

    private final static  String OPERATON_SAVE = "save";
    private final static  String OPERATON_REMOVE = "remove";
    @RequestMapping(value = "/getColData", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson getColData( @RequestParam(value = "pageId", required = true, defaultValue = "") String pageId
            ,@RequestParam(value = "src", required = true, defaultValue = "") String src
            , HttpServletRequest request
            ,HttpServletResponse response) {
        String userName = UserUtils.getUserName();
        //获取系统默认列
        List<PageColDto> systemColList =  systemPageColumnService.getSystemColList(pageId);
        //获取用户配置列
        List<PageColDto> userColList = systemPageColumnService.getUserDefaultColList(pageId,userName,true);

        //移除已选择元素
        Iterator<PageColDto> it = systemColList.iterator();
        while(it.hasNext()){
            PageColDto sysCol = it.next();
            for(PageColDto uesrCole:userColList){
                if(sysCol.getId().equals(uesrCole.getId())){
                    it.remove();
                }
            }
        }
        Map<String,List<PageColDto>> colMap = new HashMap<String,List<PageColDto>>(16);
        colMap.put("systemColList",systemColList);
        colMap.put("userColList",userColList);
        AjaxJson ajaxJson  = AjaxJson.success("",colMap);
        return ajaxJson;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson save( @RequestParam(value = "pageId", required = true, defaultValue = "") String pageId
            ,@RequestParam(value = "src", required = true, defaultValue = "") String src
            , HttpServletRequest request
            ,HttpServletResponse response) {
        String userName = UserUtils.getUserName();
        String systemIds = request.getParameter("id");
        List<String> idList = Arrays.asList(systemIds.split(","));
        AjaxJson ajaxJson = AjaxJson.success("操作成功");
        try {
            //删除所有配置
            List<UserPageColumn> userPageColumnList = userPageColumnService.findListByColId(pageId,systemIds);
            if(OPERATON_REMOVE.equals(src)){
                //删除所有配置
                userPageColumnService.removeAll(userPageColumnList);
            }
            if(OPERATON_SAVE.equals(src)){
                List<SystemPageColumn> systemPageColumnList = systemPageColumnService.findListById(pageId,systemIds);
                Map<String,SystemPageColumn> systemPageColumnMap = new HashMap<String,SystemPageColumn>(16);
                for(SystemPageColumn systemPageColumn:systemPageColumnList){
                    systemPageColumnMap.put(ConvertUtil.null2String(systemPageColumn.getId()),systemPageColumn);
                }
                //删除所有配置
                userPageColumnService.removeAll(userPageColumnList);
                int i= 0;
                List<UserPageColumn> userPageColumns = new ArrayList<UserPageColumn>();
                for(String id:idList){
                    SystemPageColumn systemPageColumn = systemPageColumnMap.get(id);
                    if(systemPageColumn != null){
                        UserPageColumn userPageColumn = new UserPageColumn();
                        userPageColumn.setPageId(systemPageColumn.getPageId());
                        userPageColumn.setUserName(userName);
                        userPageColumn.setColId(systemPageColumn.getId());
                        userPageColumn.setOrders(i++);
                        userPageColumn.setColWidth(systemPageColumn.getColWidth());
                        userPageColumns.add(userPageColumn);
                    }
                }
                if(userPageColumns != null && userPageColumns.size() > 0){
                    userPageColumnService.saveAll(userPageColumns);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            ajaxJson =AjaxJson.fail("操作失败："+e.getMessage());
        }
        return ajaxJson;
    }
}
